package com.lanswon.icc.auth.service;

import com.lanswon.icc.common.core.constant.ResultConstant;
import com.lanswon.icc.common.core.constant.SecurityConstant;
import com.lanswon.icc.common.core.constant.StatusConstant;
import com.lanswon.icc.common.core.dto.UserInfo;
import com.lanswon.icc.common.core.po.admin.SysUser;
import com.lanswon.icc.common.core.vo.Result;
import com.lanswon.icc.common.rpc.feign.AdminServiceFeign;
import lombok.AllArgsConstructor;
import org.springframework.security.authentication.InternalAuthenticationServiceException;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

@Service
@AllArgsConstructor
public class MyUserDetailService implements UserDetailsService {
    private final AdminServiceFeign adminServiceFeign;
//
//    @Autowired
//    ErrorLoginService errorLoginService;

    @Override
    public UserDetails loadUserByUsername(String username) {

//        errorLoginService.loclLoginCheck();

        UserDetails userDetails = null;
        Result<UserInfo> result = adminServiceFeign.findByUsername(username, SecurityConstant.FROM_IN);
        if(result.getCode() == ResultConstant.EX_INTERFACE){
            throw new InternalAuthenticationServiceException(result.getMessage()+",无法认证");
        }else if(result.getCode() == ResultConstant.EX_DATA_IS_NULL){
            throw new UsernameNotFoundException("这里不需写异常信息（写了也没用），内部实现了去配置文件里找相关异常信息");
        }

        UserInfo userInfo = (UserInfo) result.getData();

        SysUser sysUser = userInfo.getSysUser();


        List<String> authList = new ArrayList<>();

        List<String> roleCodeList = userInfo.getRoleCode();//角色信息
        if(roleCodeList.size() > 0){
        for(String roleCode: roleCodeList){
                roleCode = "ROLE_"+roleCode;
                authList.add(roleCode);
            }
        }
        if(userInfo.getPermissions().size() > 0){
            authList.addAll(userInfo.getPermissions());
        }

        Collection<? extends GrantedAuthority> authorities
                = AuthorityUtils.createAuthorityList(authList.toArray(new String[0]));


//        boolean expiredFlag = true;
//        int flag = sysUser.getExpiredFlag();
//        if(flag == StatusConstant.EXPIRED_FALG_Y){
//            expiredFlag = false;
//        }else if(flag == StatusConstant.EXPIRED_FALG_NOT){
//            Date now = new Date();
//            expiredFlag = sysUser.getStartTime().before(now) && sysUser.getEndTime().after(now);
//        }

        //自定义锁定拦截，未使用 user中的lock
        userDetails = new User(sysUser.getUsername(),sysUser.getPassword(),Integer.parseInt(sysUser.getEnabled()) == StatusConstant.ENABLE_FLAG_Y,true,true,true,authorities);
        return userDetails;
    }


}
